package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Image;
import com.tbit.uqbike.client.pojo.ParkPointImage;
import com.tbit.uqbike.client.service.ImageService;
import com.tbit.uqbike.client.service.ParkPointImageService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 停车点图片
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Controller
@RequestMapping("/parkPointImage")
public class ParkPointImageController {
    @Autowired
    private ParkPointImageService parkPointImageService;
    @Autowired
    private ImageService imageService;


    /**
     * 根据停车点id查询停车点图片
     *
     * @param parkPointId 停车点id
     * @param session     会话
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/getByParkPointId")
    public void getByParkPointId(Integer parkPointId, HttpSession session, HttpServletRequest request,
                                 HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(parkPointId, "-120001", "parkPointId不能为空");

            List<ParkPointImage> parkPointImages = parkPointImageService.getByParkPointId(parkPointId);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, parkPointImages);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 查看图片
     *
     * @param imageId  图片id
     * @param session  会话
     * @param response 响应
     * @throws IOException ioexception
     * @ignoreParams request
     */
    @RequestMapping("/getByImageId")
    public void getByImageId(String imageId, HttpSession session, HttpServletRequest request,
                             HttpServletResponse response) throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.hasLength(imageId, "-120001", "imageId不能为空");

            /**从数据库加载图片*/
            Image image = imageService.getByImageId(imageId);
            if (image == null) {
                throw new BaseException("-120003", "指定图片不存在");
            }

            if (image.getImageContent().length > 0) {
                response.setContentType("image/" + image.getImageType());
                OutputStream out = response.getOutputStream();
                try {
                    out.write(image.getImageContent());
                } catch (Exception e) {
                    e.getMessage();
                } finally {
                    out.flush();
                    out.close();
                    image = null;
                }
            }
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
            OutputUtil.renderText(request, response, map);
        }
    }

}